package com.gulimall.ware.dao;

import com.common.to.WareLockTo;
import com.gulimall.ware.entity.WareSkuEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 商品库存
 * 
 * @author Rookie-886
 * @email ${email}
 * @date 2021-03-14 22:37:36
 */
@Mapper
public interface WareSkuDao extends BaseMapper<WareSkuEntity> {

    @Update("update wms_ware_sku set stock = stock+#{skuNum} where sku_id+#{skuId} and ware_id=#{wareId}")
    int addStock(@Param("skuId") Long skuId, @Param("wareId") Long wareId, @Param("skuNum") Integer skuNum);

    @Select("select sum(stock-stock_locked) from wms_ware_sku where sku_id = #{skuId}")
    Integer getStockListBySkuIdList(Long skuId);

    @Select("select ware_id from wms_ware_sku where sku_id = #{skuId} and stock-stock_locked>=#{num}")
    List<Long> getAvailWareIdList(@Param("skuId") Long skuId, @Param("num") Integer num);

    @Update("update wms_ware_sku set stock_locked = stock_locked + #{num} where ware_id = #{wareId} and sku_id = #{skuId}")
    int lockStock(@Param("skuId") Long skuId, @Param("wareId") Long wareId, @Param("num") Integer num);

    /**
     * 解锁库存
     * @param skuId
     * @param skuNum
     * @param wareId
     * @return
     */
    @Update("update wms_ware_sku set stock_locked = stock_locked - #{skuNum} where ware_id = #{wareId} and sku_id = #{skuId}")
    int releaseStock(@Param("skuId") Long skuId, @Param("skuNum") Integer skuNum, @Param("wareId") Long wareId);
}
